package com.erp.gather.controller;

import com.erp.gather.service.IAeAutoProductService;
import com.erp.gather.service.IAeProductService;
import com.erp.gather.service.IErpCollectProductAUploadTestService;
import com.erp.gather.service.IErpCollectProductUploadTestService;
import com.ruoyi.gather.api.domain.dto.AeAutoProductDto;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.gather.api.domain.dto.ComputeFreightDto;
import com.ruoyi.shop.api.RemoteSellerStoreService;
import com.ruoyi.shop.api.domain.vo.ErpSellerStoreEmpowerVo;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gather/AeProduct")
public class AeProductController extends BaseController {
    @Autowired
    RestTemplate restTemplate;

    private final IAeProductService iAeProductService;

    private final IAeAutoProductService iAeAutoProductService;

    @Resource
    RemoteSellerStoreService remoteSellerStoreService;

    private final IErpCollectProductAUploadTestService iErpCollectProductAUploadTestService;


    /**
     * 通过类目信息获取欧盟负责人信息
     */
    @GetMapping("/getEUResponsiblePersonByCategoryId")
    public R<String> getEUResponsiblePersonByCategoryId(Long categoryId, Long sellerStoreId, Integer type) {
        String euResponsiblePersonByCategoryId = iAeProductService.getEUResponsiblePersonByCategoryId(categoryId, sellerStoreId, type);
        return R.ok(euResponsiblePersonByCategoryId);
    }

    /**
     * 获取服务模板 TODO 店铺服务模版 内部类调用
     */
//    @GetMapping("/queryPromiseTemplateById")
//    public R<String> queryPromiseTemplateById(@Param("templateId") Integer templateId, @Param("sellerId") Long sellerId) {
//        Map<String, Object> map = new HashMap<>();
//        if (StringUtils.isEmpty(checkSellerId(sellerId))) {
//            return R.ok("店铺未授权");
//        }
//        return iAeProductService.queryPromiseTemplateById(templateId, sellerId);
//    }

    /**
     * 获取店铺运费模板 TODO 运费模版 内部类调用
     */
//    @GetMapping("/getSellerStoreFreightTemplate")
//    public R<String> getSellerStoreFreightTemplate(Long sellerId) {
//        //发送请求
//        if (StringUtils.isEmpty(checkSellerId(sellerId))) {
//            return R.fail("店铺未授权");
//        }
//        return iAeProductService.getSellerStoreFreightTemplate(sellerId);
//    }

    /**
     * 获取店铺运费模板发货地 TODO 运费模版 内部类调用
     */
//    @GetMapping("/getFreightTemplateLogisticsCompany")
//    public R<Object> getFreightTemplateLogisticsCompany(Long templateId,Long storeId) {
//        return iAeProductService.getFreightTemplateLogisticsCompany(templateId,storeId);
//    }
    public String checkSellerId(Long sellerId) {
        //确认店铺是否存在，查看卖家店铺授权表
        R<Object> objectR = remoteSellerStoreService.getSellerStoreInfoById(sellerId);
        ErpSellerStoreEmpowerVo storeEmpower = (ErpSellerStoreEmpowerVo) objectR.getData();
        //根据店铺id，找到对应店铺并确认该店铺是否已经授权
        if (storeEmpower != null && storeEmpower.getIsEmpower().equals("1")) {
            return sellerId.toString();
        } else {
            return null;
        }
    }

    /**
     * 测试图片银行
     */
    @GetMapping("/bankPic")
    public void bankPic(@Param("productId") String productId) {
//        iAeProductService.getAllPicByProductId(Long.valueOf(productId));
        iAeProductService.getAllPicByProductTest(Long.valueOf(productId));
    }

    /**
     * 通过类目信息获取有权限的店铺
     */
    @GetMapping("/getCategoryTreeListForStore")
    public R<String> getCategoryTreeListForStore(Long categoryId) {
        Long sellerStoreId = iAeProductService.getCategoryTreeListForStore(categoryId);
        return R.ok(sellerStoreId.toString());
    }

    /**
     * 创建测试店铺
     */
    @GetMapping("/createProductAutoTestStore")
    public R<Object> createProductAutoTestStore(Long storeId) {
        return iAeProductService.createProductAutoTestStore(storeId);
    }

    /**
     * 商品上架/下架
     */
    @GetMapping("/productOfflineOrOnline")
    public R<Object> productOfflineOrOnline(@RequestParam("sellerStoreProductIds") String sellerStoreProductIds, @RequestParam("sellerId") String sellerId,@RequestParam("type") String type) {
        return iAeProductService.productOfflineOrOnline(sellerStoreProductIds, Long.parseLong(sellerId), Integer.parseInt(type));

    }

    /**
     * 删除商品
     */
    @GetMapping("/deleteProductToAe")
    public R<Object> deleteProductToAe(Long productId, Long sellerId) {
        return iAeProductService.deleteProductToAe(productId, sellerId);
    }

    /**
     * 查询商家某个商品详情
     *
     * @param productId
     * @param sellerId
     * @return
     */
    @GetMapping("/queryAeProduct")
    public R<Object> queryAeProduct(Long productId, Long sellerId) {
        return iAeProductService.queryAeProduct(productId, sellerId);
    }

    @GetMapping("/productOfflineForUpdateTest")
    public R<Object> productOfflineForUpdateTest(String sellerStoreProductIds, Long sellerId, Integer type) {
        return iAeProductService.productOfflineForUpdateTest(sellerStoreProductIds, sellerId, type);
    }
//
//    /**
//     * 快速测试发布
//     *
//     * @return
//     */
//    @PostMapping("/uploadTestCollectProductAQuick")
//    public R<Object> uploadTestCollectProductAQuick(@RequestBody ComputeFreightDto dto) {
//        return iErpCollectProductAUploadTestService.uploadTestCollectProductAQuick(dto);
//    }
//
//    /**
//     * 新增调价任务
//     */
//    @GetMapping("/addProductDiscountAdjustmentTask")
//    public R<Object> addProductDiscountAdjustmentTask(Long aeProductId) {
//        return iAeProductService.addProductDiscountAdjustmentTask(aeProductId);
//    }
//
//    /**
//     * 从速卖通获取类目所需要的资质信息
//     */
//    @GetMapping("/queryCategoryQualificationList")
//    public R<Object> queryCategoryQualificationList(Long categoryId) {
//        return iAeProductService.queryCategoryQualificationList(categoryId);
//    }
//
//    @GetMapping("/queryProductPostType")
//    public R<Object> queryProductPostType(String storeId,String collectProductId) {
//        return iAeProductService.queryProductPostType(storeId,collectProductId);
//    }
//
//    /**
//     * 店铺商品上传接口 POSTMAN用
//     * 店铺商品发布接口
//     *
//     * @return
//     */
//    @GetMapping("/postProductToAe")
//    public R<String> postProductToAe(Long sellerStoreProductId, Long sellerId,Integer type) {
//        return iAeProductService.disposeProductData(sellerStoreProductId, sellerId,type);
//    }
//
//    /**
//     * 同步个人商品库商品（双十一2.0）
//     */
//    @GetMapping("/synchronizationProductEdit")
//    public R<Object> synchronizationProductEdit(Long collectProductId) {
//        return iAeProductEditService.synchronizationProductEdit(collectProductId);
//    }
//
//    /**
//     * 商品修改
//     *
//     * @param sellerStoreProductId
//     * @param sellerId
//     * @return
//     */
//    @GetMapping("/productEdit")
//    public R<Object> productEdit(Long sellerStoreProductId, Long sellerId,Integer postType) {
//        return iAeProductEditService.productEdit(sellerStoreProductId, sellerId,postType);
//    }
//
//    /**
//     * 商品修改测试，同时上传图片银行，适用于同步2.0商品和不良信息诊断优化
//     *
//     * @param sellerStoreProductId
//     * @return
//     */
//    @GetMapping("/productEditAndUploadPicBank")
//    public R<Object> productEditAndUploadPicBank(Long sellerStoreProductId,Integer postType) {
//        return iAeProductEditService.productEditAndUploadPicBank(sellerStoreProductId,postType);
//    }


//    /**
//     * 运费试算
//     *
//     * @param aeProductFreight
//     * @return
//     */
//    @PostMapping("/calculateFreight")
//    public R<String> calculateFreight(@RequestBody AeProductFreight aeProductFreight) {
//        //查看本地表有无完全相同数据
//        AeProductFreight aeProductFreightSel = iAeProductFreightService.getSameFreight(aeProductFreight);
//        if (aeProductFreightSel != null) {
//            //已经有了相同的数据
//            R.ok(aeProductFreightSel.getFreight());
//        } else {
//            //发送请求
//            if (StringUtils.isEmpty(checkSellerId(aeProductFreight.getSellerId()))) {
//                return R.ok("店铺未授权");
//            }
//            //Ae接口
//            String returnUrl = aeConfig.getEndpoint() + "/erp/erpProduct/getCalculateFreight";
//            ResponseEntity<String> forEntity = restTemplate
//                    .postForEntity(returnUrl, aeProductFreight, String.class);
//            String body = (String) forEntity.getBody();
//            Integer code = JSONObject.parseObject(body).getInteger("code");
//            if (!code.equals(500)) {
//                //将数据存入本地表中
//                iAeProductFreightService.addProductFreight(aeProductFreight, new BigDecimal(body));
//                return R.ok(body);
//            } else {
//                return R.ok("获取失败");
//            }
//        }
//        return R.ok("试算成功");
//    }
//    /**
//     * 运费试算
//     *
//     * @param aeProductFreight
//     * @return
//     */
//    @PostMapping("/calculateProFreight")
//    public R<String> calculateProFreight(@RequestBody AeProductFreight aeProductFreight) {
//        //查看本地表有无完全相同数据
//        AeProductFreight aeProductFreightSel = iAeProductFreightService.getSameFreight(aeProductFreight);
//        if (aeProductFreightSel != null) {
//            //已经有了相同的数据
//            R.ok(aeProductFreightSel.getFreight());
//        } else {
//            //发送请求
//            if (StringUtils.isEmpty(checkSellerId(aeProductFreight.getSellerId()))) {
//                return R.ok("店铺未授权");
//            }
//            //Ae接口
//            String returnUrl = aeConfig.getEndpoint() + "/erp/erpProduct/getCalculateFreight";
//            ResponseEntity<String> forEntity = restTemplate
//                    .postForEntity(returnUrl, aeProductFreight, String.class);
//            String body = (String) forEntity.getBody();
//            Integer code = JSONObject.parseObject(body).getInteger("code");
//            if (!code.equals(500)) {
//                //将数据存入本地表中
//                iAeProductFreightService.addProductFreight(aeProductFreight, new BigDecimal(body));
//                return R.ok(body);
//            } else {
//                return R.ok("获取失败");
//            }
//        }
//        return R.ok("试算成功");
//    }


    /**
     * 新版自动化接口准备工作
     */
    @PostMapping("/publishAeProduct")
    public R<Object> publishAeProduct(@RequestBody AeAutoProductDto autoProductDto) {
        return iAeAutoProductService.publishAeProduct(autoProductDto);
    }
    /**
     * 新版测试发布接口准备工作
     */
    @PostMapping("/uploadTestProduct")
    public R<Object> uploadTestProduct(@RequestBody ComputeFreightDto dto) {
        return iAeAutoProductService.uploadTestProduct(dto);
    }




    /**
     * 新版自动化接口补发消息队列
     */
    @PostMapping("/reissueMessageQueue")
    public R<Object> reissueMessageQueue(@RequestBody AeAutoProductDto autoProductDto) {
        return iAeAutoProductService.reissueMessageQueue(autoProductDto);
    }

    /**
     * 校验侵权词
     */
    @GetMapping("/checkViolationWords")
    public R<Object> checkViolationWords(String newSubject) {
        return iAeAutoProductService.checkViolationWords(newSubject);
    }


}
